package com.way.activity;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.way.bean.MessageBean;
import com.way.dialer.R;
import com.way.swipeback.SwipeBackActivity;
import com.way.view.adapter.MessageBoxListAdapter;

public class MessageBoxActivity extends SwipeBackActivity {

	private ListView mTalkListView;
	private List<MessageBean> mMsgBeanLists = null;
	private Button mSendBtn;
	private ImageView mReturnBtn;
	private ImageView mCallBtn;
	private EditText mMsgEditText;
	private SimpleDateFormat mSimpleDateFormat;
	private AsyncQueryHandler mAsyncQueryHandler;
	private String mAddress;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_messageboxlist);

		mReturnBtn = (ImageView) findViewById(R.id.btn_return);
		mCallBtn = (ImageView) findViewById(R.id.btn_call);
		mSendBtn = (Button) findViewById(R.id.fasong);
		mMsgEditText = (EditText) findViewById(R.id.neirong);

		String thread = getIntent().getStringExtra("threadId");
		mAddress = getIntent().getStringExtra("phoneNumber");
		TextView tv = (TextView) findViewById(R.id.topbar_title);
		tv.setText(getPersonName(mAddress));

		mSimpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");

		init(thread);

		mReturnBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				MessageBoxActivity.this.setResult(RESULT_OK);
				MessageBoxActivity.this.finish();
			}
		});
		mCallBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Uri uri = Uri.parse("tel:" + mAddress);
				Intent it = new Intent(Intent.ACTION_CALL, uri);
				startActivity(it);
			}
		});
		mSendBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String nei = mMsgEditText.getText().toString();
				ContentValues values = new ContentValues();
				values.put("address", mAddress);
				values.put("body", nei);
				getContentResolver().insert(Uri.parse("content://sms/sent"),
						values);
				Toast.makeText(MessageBoxActivity.this, nei, Toast.LENGTH_SHORT)
						.show();
			}
		});
	}

	private void init(String thread) {

		mAsyncQueryHandler = new MyAsyncQueryHandler(getContentResolver());

		mTalkListView = (ListView) findViewById(R.id.list);
		mMsgBeanLists = new ArrayList<MessageBean>();

		Uri uri = Uri.parse("content://sms");
		String[] projection = new String[] { "date", "address", "person",
				"body", "type" };// 查询的列
		mAsyncQueryHandler.startQuery(0, null, uri, projection, "thread_id = "
				+ thread, null, "date asc");
	}

	/**
	 * 数据库异步查询类AsyncQueryHandler
	 * 
	 * @author administrator
	 * 
	 */
	private class MyAsyncQueryHandler extends AsyncQueryHandler {

		public MyAsyncQueryHandler(ContentResolver cr) {
			super(cr);
		}

		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				for (int i = 0; i < cursor.getCount(); i++) {
					cursor.moveToPosition(i);
					String date = mSimpleDateFormat.format(new Date(cursor
							.getLong(cursor.getColumnIndex("date"))));
					if (cursor.getInt(cursor.getColumnIndex("type")) == 1) {
						MessageBean d = new MessageBean(
								cursor.getString(cursor
										.getColumnIndex("address")),
								date,
								cursor.getString(cursor.getColumnIndex("body")),
								R.layout.list_say_he_item);
						mMsgBeanLists.add(d);
					} else {
						MessageBean d = new MessageBean(
								cursor.getString(cursor
										.getColumnIndex("address")),
								date,
								cursor.getString(cursor.getColumnIndex("body")),
								R.layout.list_say_me_item);
						mMsgBeanLists.add(d);
					}
				}
				if (mMsgBeanLists.size() > 0) {
					mTalkListView.setAdapter(new MessageBoxListAdapter(
							MessageBoxActivity.this, mMsgBeanLists));
					mTalkListView.setSelection(mMsgBeanLists.size());
				} else {
					Toast.makeText(MessageBoxActivity.this, "没有短信进行操作",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	public String getPersonName(String number) {
		String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME, };
		Cursor cursor = this.getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				projection,
				ContactsContract.CommonDataKinds.Phone.NUMBER + " = '" + number
						+ "'", null, null);
		if (cursor == null) {
			return number;
		}
		String name = number;
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			name = cursor.getString(cursor
					.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
		}
		cursor.close();
		return name;
	}

}
